﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using StructureMap;

namespace ViewModel
{
    /// <summary>
    /// The viewmodel for the main window.
    /// </summary>
    public class MainWindowViewModel : WorkspaceViewModel
    {

        private ReadOnlyCollection<CommandViewModel> _featureCommands;
        private ObservableCollection<SectionWorkspaceViewModel> _sections;
        private ObservableCollection<WorkspaceViewModel> _workspaces;
        private WorkspaceViewModel currentWorkspace;

        public ReadOnlyCollection<CommandViewModel> FeatureCommands
        {
            get
            {
                if(_featureCommands == null)
                {
                    List<CommandViewModel> cmds = CreateFeatureCommands();
                    _featureCommands = new ReadOnlyCollection<CommandViewModel>(cmds);
                }
                return _featureCommands;
            }
        }

        List<CommandViewModel> CreateFeatureCommands()
        {
            return new List<CommandViewModel>
                       {
                           new CommandViewModel( "Inventory", new RelayCommand(param => ShowFeatureSection(ObjectFactory.GetInstance<Inventory.SectionViewModel>()))),
                           new CommandViewModel( "Finance", new RelayCommand(param => ShowFeatureSection(ObjectFactory.GetInstance<Finance.SectionViewModel>()))),
                       };
        }

        private void ShowFeatureSection(SectionWorkspaceViewModel section)
        {
            Sections.Clear();
            Sections.Add(section);
        }

        public void ShowWorkspace(WorkspaceViewModel workspace)
        {
            Workspaces.Clear();
            Workspaces.Add(workspace);
//            currentWorkspace = workspace;
        }

        public WorkspaceViewModel CurrentWorkspace 
        { 
            get
            {
                return currentWorkspace;
            }
        }

        /// REFACTOR: need to convert this from a collection to a single section control. Having trouble with the bind part of it.
        public ObservableCollection<SectionWorkspaceViewModel> Sections
        {
            get
            {
                if(_sections == null)
                {
                    _sections = new ObservableCollection<SectionWorkspaceViewModel>();
                }
                return _sections;
            }
        }

        /// REFACTOR: need to convert this from a collection to a single workspace control. Having trouble with the bind part of it.
        /// <summary>
        /// Returns the collection of available workspaces to display.
        /// A 'workspace' is a ViewModel that can request to be closed.
        /// </summary>
        public ObservableCollection<WorkspaceViewModel> Workspaces
        {
            get
            {
                if(_workspaces == null)
                {
                    _workspaces = new ObservableCollection<WorkspaceViewModel>();
                }
                return _workspaces;
            }
        }


        public void ExportData()
        {
        }
    }
}